package com.yonyou.pmclouds.signin.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.DateUtils;
import com.yonyou.pmclouds.signin.entity.QueryListParam;
import com.yonyou.pmclouds.signin.entity.SignInListViewVO;
import com.yonyou.pmclouds.signin.entity.SignInVO;
import com.yonyou.pmclouds.signin.rmiitf.SignInMaintain;
import com.yonyou.pmclouds.signin.rmiitf.SignInQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mobile/signin")
public class SignInMobController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private SignInQuery signInQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private SignInMaintain maintain;


    /**
     * 提交
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public SignInVO add(@RequestBody SignInVO signInVO) throws BusinessException {
        if(signInVO == null) return null;
        if (StringUtils.isEmpty(signInVO.getPkProject()))
            throw new BusinessException("项目不能为空");

        return maintain.insert(signInVO);
    }


    /**
     * 获取当天所有已签到信息列表
     */
    @RequestMapping(value = "/getCurrentDayList")
    @ResponseBody
    public SignInListViewVO[] getCurrentDayList(@RequestBody QueryListParam queryListParam) throws BusinessException {
        if(queryListParam == null || StringUtils.isEmpty(queryListParam.getPkTenant()) || StringUtils.isEmpty(queryListParam.getPkUser())){
            return new SignInListViewVO[0];
        }
        Calendar cal = Calendar.getInstance();
        Map<String, List<SignInListViewVO>> resultMap = signInQuery.getSignInListTotalInfo(queryListParam.getPkTenant(),
                DateUtils.asDateBegin(cal.getTime()), DateUtils.asDateEnd(cal.getTime()), new String[]{queryListParam.getPkUser()});
        if(resultMap != null && resultMap.get(queryListParam.getPkUser()) != null){
            return resultMap.get(queryListParam.getPkUser()).toArray(new SignInListViewVO[0]);
        }
        return new SignInListViewVO[0];
    }

    /**
     * 获取区间内已签到的信息列表
     */
    @RequestMapping(value = "/getList")
    @ResponseBody
    public List<List<SignInListViewVO> > getList(@RequestBody QueryListParam queryListParam) throws BusinessException {
        if(queryListParam == null || StringUtils.isEmpty(queryListParam.getPkTenant()) ||
                StringUtils.isEmpty(queryListParam.getPkUser()) || queryListParam.getStartDate() == null || queryListParam.getEndDate() == null){
            return null;
        }
        Map<String, List<List<SignInListViewVO>> > resultMap = signInQuery.getSignInListInfo(queryListParam.getPkTenant(),
                DateUtils.asDateBegin(queryListParam.getStartDate()), DateUtils.asDateEnd(queryListParam.getEndDate()), new String[]{queryListParam.getPkUser()});
        if(resultMap != null && resultMap.get(queryListParam.getPkUser()) != null){
            return resultMap.get(queryListParam.getPkUser());
        }
        return null;
    }
}
